<?php
/**
 * Notes:
 * User: LiaoHong
 * Date: 2025/10/20
 * Time: 9:04
 **/
namespace App\Services\User;
use App\Models\User\Dept;

class PermissionService
{
    private $title = '部门关联';
    /**
     * @Notes:添加部门
     * @param $params
     * @return bool
     * @author: Liao Hong
     * @Time: 2025/10/20   11:11
     * @Interface add
     */
    public function add($params)
    {
        $row = Dept::query()->insert($params);
        addLog($this->title,'1',$row);
        return $row;
    }

    /**
     * @Notes:获取部门结构树
     * @author: Liao Hong
     * @Time: 2025/10/22   9:08
     * @Interface deptTree
     */
    public function deptTree()
    {
        $row = Dept::query()->select(['dept_id as id','dept_name as menu_name','parent_id'])->get()->toArray();
        $result = buildTree($row);
        return $result;

    }

    public function getRow($params)
    {
        return Dept::query()->find($params['dept_id'])->toArray();
    }

    /**
     * @Notes:修改部门
     * @param $parmas
     * @author: Liao Hong
     * @Time: 2025/10/20   11:12
     * @Interface update
     */
    public function update($parmas){
        $row = Dept::query()->where('dept_id',$parmas['dept_id'])->update($parmas);
        addLog($this->title,'2',$row);
        return Dept::query()->where('dept_id',$parmas['dept_id'])->update($parmas);

    }
    /**
     * @Notes:删除部门
     * @param $parmas
     * @author: Liao Hong
     * @Time: 2025/10/20   11:12
     * @Interface update
     */
    public function delete($parmas){
        $row = Dept::query()->where('dept_id',$parmas['dept_id'])->delete();
        addLog($this->title,'3',$row);
        return $row;
    }
    /**
     * @Notes:部门列表
     * @param $parmas
     * @author: Liao Hong
     * @Time: 2025/10/20   11:12
     * @Interface update
     */
    public function list($parmas){
      $query = [];
      if(isset($parmas['dept_name']) && !empty($parmas['dept_name'])){
          $query[] = ['dept_name','like','%'.$parmas['dept_name'].'%'];
      }
      if(isset($parmas['status']) && !empty($parmas['status'])){
          $query[] = ['status','=',$parmas['status']];
      }
      $row = Dept::query()->where($query)->get();
      $row = empty($row) ?  [] :$row->toArray();
      return $row;
    }

}
